//
//  EmoticonViewModel.swift
//  微博分支表情键盘
//
//  Created by JJ on 15/9/12.
//  Copyright © 2015年 JJ. All rights reserved.
//

import UIKit

class EmoticonViewModel{
    
    static let shareViewModel=EmoticonViewModel()
    
    init(){
        
        loadPackage()
    }
    func emoticonText(str:String,font:UIFont)->NSMutableAttributedString{
    
        let pattern="\\[.*?\\]"
        let reger = try!NSRegularExpression(pattern: pattern, options: [])
        
        let results = reger.matchesInString(str, options: [], range: NSRange(location: 0, length: str.characters.count))
        var count = results.count
        
        let strM=NSMutableAttributedString(string: str)
        while count > 0 {
            
            let range = results[ --count].rangeAtIndex(0)
            
            let emString = (str as NSString).substringWithRange(range)
            if let em = EmoticonViewModel.shareViewModel.emoticon(emString){
                
                let imageText=EmoticonAttachment.emoticonAttranbutetText(em, font: font)
                
                strM.replaceCharactersInRange(range, withAttributedString: imageText)
            }
        }
        
         return strM
    
    }
    
    func emoticon(string:String)->Emoticon?{
        
        var em:Emoticon?
        for p in EmoticonViewModel.shareViewModel.packages{
            
            em = p.emoticons.filter({ (e) -> Bool in
                return e.chs == string
            }).last
            
            if em != nil{
                break
            }
        }
        
        return em
    }
    
    func favourate(indexPath:NSIndexPath){
        
        if indexPath.section==0{
            return
        }
        
        let em=emoticon(indexPath)
        em.times++
        if !packages[0].emoticons.contains(em){
            packages[0].emoticons.insert(em, atIndex: 0)
        }
        packages[0].emoticons.sortInPlace { (obj1, obj2) -> Bool in
            return obj1.times > obj2.times
        }
        if packages[0].emoticons.count > 21{
        
            packages[0].emoticons.removeAtIndex(20)
        }
        
    }
    
    lazy var packages=[EmoticonPackage]()
    
    func loadPackage(){
        
        packages.append(EmoticonPackage(dic: ["group_name_cn":"最近使用"]))
        
        let path=NSBundle.mainBundle().pathForResource("emoticons.plist", ofType: nil, inDirectory: "Emoticons.bundle")
        
        let dic=NSDictionary(contentsOfFile: path!)
        
        let array=dic!["packages"] as![[String:AnyObject]]
        
        for infoDic in array{
            
            let id = infoDic["id"] as! String
            
            let infoPath=NSBundle.mainBundle().pathForResource("info.plist", ofType: nil, inDirectory: "Emoticons.bundle/"+id)
            
            let d=NSDictionary(contentsOfFile:infoPath!) as! [String:AnyObject]
            
            packages.append(EmoticonPackage(dic:d))
            
        
        }
    
//        print(packages)
        
    }
     func emoticon(indexPath:NSIndexPath)->Emoticon{
        
        return packages[indexPath.section].emoticons[indexPath.item]
    }
}
